{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "from RawNet.model import RawNet\n",
    "import Config.globalconfig as Gconfig\n",
    "import yaml\n",
    "import tensorwatch as tw\n",
    "import sys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "def getSavedModel():\n",
    "    device = 'cuda' if torch.cuda.is_available() else 'cpu'\n",
    "    #已有的模型路径\n",
    "    MODEL_SAVE_PATH=Gconfig.RAW_MS_CHOICE_PATH\n",
    "    # 模型内部结构配置\n",
    "    yaml_path = Gconfig.RAW_YAML_CONFIG_PATH\n",
    "    with open(yaml_path, 'r') as f_yaml:\n",
    "        parser1 = yaml.load(f_yaml, Loader=yaml.FullLoader)\n",
    "    #\n",
    "    model = RawNet(parser1['model'], device)\n",
    "    model = (model).to(device)\n",
    "    model.load_state_dict(torch.load(MODEL_SAVE_PATH, map_location=device))\n",
    "    print('Model loaded : {}'.format(MODEL_SAVE_PATH))\n",
    "    return model"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model loaded : F:\\Attack-Transferability-On-Synthetic-Detection\\ALL_MODEL\\RAWNET\\model_30_32_1024_lr0.0003_norm\\epoch_29_99.7_99.5.pth\n"
     ]
    }
   ],
   "source": [
    "device = 'cuda' if torch.cuda.is_available() else 'cpu'\n",
    "model=getSavedModel()\n",
    "x=torch.randn(1,64600)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [],
   "source": [
    "from torchviz import make_dot"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [],
   "source": [
    "y=model(x.cuda())"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [],
   "source": [
    "g = make_dot(y)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "'espnet_model.pdf'"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g.render('espnet_model', view=False)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "RawNet(\n  (Sinc_conv): SincConv()\n  (first_bn): BatchNorm1d(20, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n  (selu): SELU(inplace=True)\n  (block0): Sequential(\n    (0): Residual_block(\n      (lrelu): LeakyReLU(negative_slope=0.3)\n      (conv1): Conv1d(20, 20, kernel_size=(3,), stride=(1,), padding=(1,))\n      (bn2): BatchNorm1d(20, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n      (conv2): Conv1d(20, 20, kernel_size=(3,), stride=(1,), padding=(1,))\n      (mp): MaxPool1d(kernel_size=3, stride=3, padding=0, dilation=1, ceil_mode=False)\n    )\n  )\n  (block1): Sequential(\n    (0): Residual_block(\n      (bn1): BatchNorm1d(20, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n      (lrelu): LeakyReLU(negative_slope=0.3)\n      (conv1): Conv1d(20, 20, kernel_size=(3,), stride=(1,), padding=(1,))\n      (bn2): BatchNorm1d(20, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n      (conv2): Conv1d(20, 20, kernel_size=(3,), stride=(1,), padding=(1,))\n      (mp): MaxPool1d(kernel_size=3, stride=3, padding=0, dilation=1, ceil_mode=False)\n    )\n  )\n  (block2): Sequential(\n    (0): Residual_block(\n      (bn1): BatchNorm1d(20, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n      (lrelu): LeakyReLU(negative_slope=0.3)\n      (conv1): Conv1d(20, 128, kernel_size=(3,), stride=(1,), padding=(1,))\n      (bn2): BatchNorm1d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n      (conv2): Conv1d(128, 128, kernel_size=(3,), stride=(1,), padding=(1,))\n      (conv_downsample): Conv1d(20, 128, kernel_size=(1,), stride=(1,))\n      (mp): MaxPool1d(kernel_size=3, stride=3, padding=0, dilation=1, ceil_mode=False)\n    )\n  )\n  (block3): Sequential(\n    (0): Residual_block(\n      (bn1): BatchNorm1d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n      (lrelu): LeakyReLU(negative_slope=0.3)\n      (conv1): Conv1d(128, 128, kernel_size=(3,), stride=(1,), padding=(1,))\n      (bn2): BatchNorm1d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n      (conv2): Conv1d(128, 128, kernel_size=(3,), stride=(1,), padding=(1,))\n      (mp): MaxPool1d(kernel_size=3, stride=3, padding=0, dilation=1, ceil_mode=False)\n    )\n  )\n  (block4): Sequential(\n    (0): Residual_block(\n      (bn1): BatchNorm1d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n      (lrelu): LeakyReLU(negative_slope=0.3)\n      (conv1): Conv1d(128, 128, kernel_size=(3,), stride=(1,), padding=(1,))\n      (bn2): BatchNorm1d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n      (conv2): Conv1d(128, 128, kernel_size=(3,), stride=(1,), padding=(1,))\n      (mp): MaxPool1d(kernel_size=3, stride=3, padding=0, dilation=1, ceil_mode=False)\n    )\n  )\n  (block5): Sequential(\n    (0): Residual_block(\n      (bn1): BatchNorm1d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n      (lrelu): LeakyReLU(negative_slope=0.3)\n      (conv1): Conv1d(128, 128, kernel_size=(3,), stride=(1,), padding=(1,))\n      (bn2): BatchNorm1d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n      (conv2): Conv1d(128, 128, kernel_size=(3,), stride=(1,), padding=(1,))\n      (mp): MaxPool1d(kernel_size=3, stride=3, padding=0, dilation=1, ceil_mode=False)\n    )\n  )\n  (avgpool): AdaptiveAvgPool1d(output_size=1)\n  (fc_attention0): Sequential(\n    (0): Linear(in_features=20, out_features=20, bias=True)\n  )\n  (fc_attention1): Sequential(\n    (0): Linear(in_features=20, out_features=20, bias=True)\n  )\n  (fc_attention2): Sequential(\n    (0): Linear(in_features=128, out_features=128, bias=True)\n  )\n  (fc_attention3): Sequential(\n    (0): Linear(in_features=128, out_features=128, bias=True)\n  )\n  (fc_attention4): Sequential(\n    (0): Linear(in_features=128, out_features=128, bias=True)\n  )\n  (fc_attention5): Sequential(\n    (0): Linear(in_features=128, out_features=128, bias=True)\n  )\n  (bn_before_gru): BatchNorm1d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n  (gru): GRU(128, 1024, num_layers=3, batch_first=True)\n  (fc1_gru): Linear(in_features=1024, out_features=1024, bias=True)\n  (fc2_gru): Linear(in_features=1024, out_features=2, bias=True)\n  (sig): Sigmoid()\n  (logsoftmax): LogSoftmax(dim=1)\n)"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "SincConv()"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.Sinc_conv"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}